/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Server.ImplementInterface;

import DAL.DBManager;
import Server.DTO.Parameter_DTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author 10520_000
 */
public class ParameterRepository {
    public static int Insert(Parameter_DTO info){
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "INSERT INTO Parameters(MinimumDeposit, MinimumAdditionalDeposit, MinimumSendTime, TimeInterest) Values(?,?,?,?)";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            /*pstmt.setInt(1, info.getMinimumDeposit());
            pstmt.setInt(2, info.getMinimumAdditionalDeposit());
            pstmt.setInt(3, info.getMinimumSendTime());
            pstmt.setInt(4, info.getTimeInterest());*/
            iRet = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static int Update(Parameter_DTO info){
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "UPDATE Parameters SET MinimumDeposit= ?, MinimumAdditionalDeposit= ?, MinimumSendTime= ?, TimeInterest= ?";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            /*pstmt.setInt(1, info.getMinimumDeposit());
            pstmt.setInt(2, info.getMinimumAdditionalDeposit());
            pstmt.setInt(3, info.getMinimumSendTime());
            pstmt.setInt(4, info.getTimeInterest());*/
            iRet = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static int Delete(Parameter_DTO info){
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "DELETE Parameters WHERE ReportID= ?";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            /*pstmt.setInt(1, info.getMinimumDeposit());
            pstmt.setInt(2, info.getMinimumAdditionalDeposit());
            pstmt.setInt(3, info.getMinimumSendTime());
            pstmt.setInt(4, info.getTimeInterest());*/
            iRet = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static Parameter_DTO FindAll() {
        Parameter_DTO info = new Parameter_DTO();

        try {
            String sql = "SELECT * FROM PARAMATER ";
            Connection con = DBManager.getInstance().getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                info.setSoTienGuiToiThieu(rs.getInt("SoTienGuiToiThieu"));
                info.setSoTienGuiThemToiThieu(rs.getInt("SoTienGuiThemToiThieu"));
                info.setThoiGianBatDauTinhLaiCoKyHan(rs.getInt("ThoiGianBatDauTinhLaiCoKyHan"));
                info.setThoiGianBatDauTinhLaiKhongKyHan(rs.getInt("ThoiGianBatDauTinhLaiKhongKyHan"));
                info.setSoLuongSoTietKiemToiDa(rs.getInt("SoLuongSoTietKiemToiDa"));
                info.setSoTienRutToiThieu(rs.getInt("SoTienRutToiThieu"));
            }

            stmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return info;
    }
    public static void main(String[] str)
    {
        System.out.println(FindAll().getSoLuongSoTietKiemToiDa());
    }
}
